package com.fengycx.controller.manager.app;

import com.fengycx.exception.GlobalException;
import com.fengycx.result.R;
import com.fengycx.templeate.FileUploadTemplate;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;

@RestController
@RequestMapping("/app")
public class UploadFile {

    @Autowired
    private FileUploadTemplate fileUploadTemplate;

    @Operation( summary= "司机认证资料上传",description = "司机认证资料上传接口")
    @Parameter(name = "file",description = "文件对象",required = true)
    @PostMapping("/uploadFile")
    public R<String> uploadFile(MultipartFile file, String folder){
        try {
            String fileName = file.getOriginalFilename();
            String realPosition = folder +"/"+ System.currentTimeMillis()+ "-"+ fileName;
            String url = fileUploadTemplate.uploadFile(file.getInputStream(), realPosition);
            return R.success(url);
        } catch (IOException e) {
            e.printStackTrace();
            throw new GlobalException(e.getMessage());
        }
    }
}
